import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from "../views/Index";
import IndexLayOut from "../components/IndexLayOut";
import {getUserInfo} from "../api/user-api"
import store from "../store";

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    component: () => import(/* webpackChunkName: "about" */ '../components/Cover')
  },
  {
    path: '/Index',
    component:IndexLayOut,
    children:[
      {
        path:'',
        component:Index
      },
      {
        path:'/Music',
        component: () => import(/* webpackChunkName: "about" */ '../views/Music')
      },
          {
            path: '/login',
            name: 'index',
            component: () => import(/* webpackChunkName: "about" */ '../views/login/components/Login')
          },
            {
              path: '/register',
              name: 'register',
              component: () => import(/* webpackChunkName: "about" */ '../views/login/components/Rgister')
            },
          {
            path: '/my',
            name: 'my',
            component: () => import(/* webpackChunkName: "about" */ '../views/login/My'),
            meta:{
              needLogin:true
            }
          },
            {
              path: '/collection',
              name: 'Collection',
              component: () => import(/* webpackChunkName: "about" */ '../views/login/components/Collection'),
              meta:{
                needLogin:true
              }
            },
            {
              path: '/view',
              name: 'view',
              component: () => import(/* webpackChunkName: "about" */ '../views/login/components/ViewIndividuals'),
              meta:{
                needLogin:true
              }
            },{
              path: '/modifyInfor',
              name: 'modifyInfor',
              component: () => import(/* webpackChunkName: "about" */ '../views/login/components/ModifyInformation'),
              meta:{
                needLogin:true
              }
            },{
              path: '/modifyPass',
              name: 'modifyPass',
              component: () => import(/* webpackChunkName: "about" */ '../views/login/components/ModifyPassword'),
              meta:{
                needLogin:true
              }
            }
    ],

    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path:'/NewsPage/:newsid',
    component: () => import(/* webpackChunkName: "about" */ '../views/components/sonrouter/NewsPage')

  },
  {
    path:'/Search',
    component: () => import(/* webpackChunkName: "about" */ '../views/components/sonrouter/Search'),
  },
  {
    path:'/SearchContent',
    name:'SearchContent',
    component: () => import(/* webpackChunkName: "about" */ '../views/components/sonrouter/SearchContent'),
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.meta.needLogin){
    //请求接口，判断登录状态
    getUserInfo().then(res=>{

      if(res.code==0){
        store.commit("changelogin",
            {islogin: true, loginName: res.data.loginName,userName: res.data.userName,phonenumber: res.data.phonenumber,avatar:res.data.avatar})
        if(store.state.islogin){
          next()
        }
      }else {
        next("/login")
      }
    })

  }else {
    next();
  }
})

export default router
